﻿using MySchool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm4
{
    public partial class Form3 : Form
    {

        private int _user_id;
        public int User_id { get => _user_id; set => _user_id = value; }

        public Form3()
        {
            InitializeComponent();
        }
        
        private void Form3_Load(object sender, EventArgs e)
        {
            DBHelper dBHelper = new DBHelper();
            dBHelper.OpenConnection();

            string sql = "select * from sys_user where id = " + User_id;

            // 创建command 对象
            SqlCommand command = new SqlCommand(sql,dBHelper.Connection);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows) {
                reader.Read();
                // 显示数据到窗体
                this.textBox1.Text = User_id.ToString();
                // 显示数据到窗体
                this.textBox2.Text = reader["password"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 更新的功能
            DBHelper dBHelper = new DBHelper();
            dBHelper.OpenConnection();
            // 获取窗体文本框的值
            int id = Convert.ToInt32(this.textBox1.Text);
            string pwd = this.textBox2.Text.Trim();
            //判断密码是有值的
            if (pwd.Equals(string.Empty)) {
                MessageBox.Show("请输入密码");
                // 让程序提前结束
                return;
            } 
            string sql = "update sys_user set password = '"+ pwd +"' where id = " + id;
            // 创建command 对象
            SqlCommand command = new SqlCommand(sql, dBHelper.Connection);
            // 定义整数变量接受方法返回值
            int count = command.ExecuteNonQuery();
            if (count > 0) {
                MessageBox.Show("保存成功！");
            }
            this.Close();
            dBHelper.CloseConnection();

        }
    }
}
